﻿using Mobileterminal.Dtos;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Mobileterminal.Forms.Zyy.Depart
{
    public  class TimelineControl: Panel
    {
        private List<DepartmentQueryss> _records; // 绑定数据库查询结果
        private class TimelineItem : UserControl
        {
            public TimelineItem(DepartmentQueryss record)
            {
                Height = 80;
                BackColor = Color.Transparent;

                // 时间节点圆圈
                var circle = new CirclePanel
                {
                    Size = new Size(12, 12),
                    Location = new Point(30, 25),
                    BackColor = GetStatusColor(record.Itemtype),
                    //BorderStyle = BorderStyle.FixedSingle
                    ForeColor = Color.Gray // 边框颜色
                };

                // 操作信息标签
                var lblInfo = new Label
                {
                    Text = $"{record.WarehousingTime:HH:mm} | {record.Itemtype} | {record.Itemname}",
                    Location = new Point(60, 10),
                    AutoSize = true
                };

                Controls.Add(circle);
                Controls.Add(lblInfo);
            }



            // 根据操作类型返回对应的颜色
            private Color GetStatusColor(string opType)
            {
                // 创建操作类型与颜色的映射字典
                var colorMap = new Dictionary<string, Color>(StringComparer.OrdinalIgnoreCase)
                {
                    { "发放接收", Color.FromArgb(76, 175, 80) },   // 绿色
                    { "开始灭菌", Color.FromArgb(33, 150, 243) },  // 蓝色
                    { "灭菌完成", Color.FromArgb(255, 152, 0) },   // 橙色
                    { "灭菌审核", Color.FromArgb(156, 39, 176) },  // 紫色
                    { "存放", Color.FromArgb(121, 85, 72) },       // 棕色
                    { "包装", Color.FromArgb(0, 188, 212) },       // 青色
                    { "异常", Color.FromArgb(244, 67, 54) }        // 红色（用于错误状态）
                };

                // 返回匹配的颜色，若未找到则返回默认灰色
                return colorMap.TryGetValue(opType, out var color)
                       ? color
                       : Color.FromArgb(158, 158, 158); // 默认灰色
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            using (var pen = new Pen(Color.Gray, 1))
            {
                e.Graphics.DrawLine(pen, 46, 0, 46, this.Height);
            }
        }

        // 动态加载数据
        public void LoadData(List<DepartmentQueryss> records)
        {
            _records = records.OrderByDescending(r => r.WarehousingTime).ToList();
            Controls.Clear();

            int yPos = 10;
            foreach (var record in _records)
            {
                var item = new TimelineItem(record)
                {
                    Location = new Point(10, yPos),
                    Width = this.Width - 25
                };
                Controls.Add(item);
                yPos += item.Height + 5;
            }
        }
    }
}
